package com.zoho.model_project.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 通用工具类
 */

@Slf4j
@UtilityClass
public class CommonUtil {

    // 时间保留到时分秒
    public String formatTime(String time) {
        return StringUtils.isNotBlank(time) ? time.substring(0, 10) : "";
    }

    // 格式化小数
    public BigDecimal formatBigDecimal(BigDecimal num, int saveCount) {
        return Objects.isNull(num) ? new BigDecimal("0") : num.setScale(saveCount, RoundingMode.HALF_UP);
    }

    // json数据，写入excel,到桌面
    public static String jsonDataToExcel(List<JSONObject> data) throws IOException {

        String desktopPath = System.getProperty("user.home") + File.separator + "Desktop";
        String filePath = desktopPath + File.separator + DateUtil.getToday() + ".xlsx";

        // 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("Data");

        // 创建居中对齐的样式
        CellStyle centeredStyle = workbook.createCellStyle();
        centeredStyle.setAlignment(HorizontalAlignment.CENTER);
        centeredStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 处理表头
        if (!data.isEmpty()) {
            JSONObject firstObj = data.get(0);
            Set<String> keys = firstObj.keySet();
            Row headerRow = sheet.createRow(0);
            int colIndex = 0;
            for (String key : keys) {
                Cell cell = headerRow.createCell(colIndex);
                cell.setCellStyle(centeredStyle);
                cell.setCellValue(key);
                colIndex++;
            }

            // 处理数据行
            int rowIndex = 1;
            for (JSONObject obj : data) {
                Row row = sheet.createRow(rowIndex);
                colIndex = 0;
                for (String key : keys) {
                    Cell cell = row.createCell(colIndex);
                    cell.setCellStyle(centeredStyle);
                    Object value = obj.get(key);
                    if (value != null) {
                        cell.setCellValue(value.toString());
                    }
                    colIndex++;
                }
                rowIndex++;
            }
        }

        // 自动调整列宽
        for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) {
            sheet.autoSizeColumn(i);
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
            String result = "数据已成功写入桌面的 Excel 文件： = " + filePath;
            System.out.println("result = " + result);
            return result;
        } finally {
            workbook.close();
        }
    }


}
